package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsTopicMapper;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DtsTopicServiceImpl implements DtsTopicService {

    @Autowired
    DtsTopicMapper topicMapper;

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String title, String subtitle) {
        IPage<DtsTopic> iPage = new Page<>(page,limit);
        QueryWrapper<DtsTopic> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)){
            wrapper.eq("title",title);
        }
        if (!StringUtils.isEmpty(subtitle)){
            wrapper.eq("subtitle",subtitle);
        }

        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        topicMapper.selectPage(iPage,wrapper);

        //封装
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return data;
    }


    @Override
//    public void insert(String title, String subtitle, String context, String price, String readCount, String picUrl) {
      public void insert(DtsTopic topic){
        topic.setAddTime(new Date());
        topic.setDeleted(false);
        topic.setUpdateTime(new Date());

        topicMapper.insert(topic);

//        DtsTopic topic = new DtsTopic();
//        topic.setTitle(title);
//        topic.setSubtitle(subtitle);
//        topic.setContent(context);
//        topic.setPrice(new BigDecimal(price));
//        topic.setReadCount(readCount);
//        topic.setPicUrl(picUrl);

        topicMapper.insert(topic);

    }

    /**
     * 删除
     * @param topic
     */
    @Override
    public void delete(DtsTopic topic) {
        topicMapper.deleteById(topic);
    }

    /**
     * 更新
     * @param topic
     */
    @Override
    public void update(DtsTopic topic) {
        topicMapper.updateById(topic);
    }
}
